<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
     public function _empty($name){
        $this->nul($name);
    }
    
    protected function nul($name){
         echo "<h1>404</h1>";
         echo "没有这个地址:".$name;
    }
    public function index(){
        $this->display();
    }
    /**
     * 发短信
     * @return [type] [description]
     */
    public function sms(){
    	if(IS_POST){
	    	$mobile=$_POST['mobile'];
	    	$send_code=$_POST['send_code'];
	    	$res=send_code($mobile,$send_code);
	    	$this->ajaxReturn($res);
    	}
    	else{
    		$arr['code']=0;
    		
    		$this->ajaxReturn('error post');
    	}

    }
    /**
     * 发邮件
     */
    public function sendemails(){
        $res=send_email('2274540015@qq.com','你已经注册了评估令账号');
        dump($res);
    }
    public function ck_show(){
        $this->display();
    }
    /**
     * 外部验证操作
     */
    public function re_ck_phone(){
        $res=I('SERVER.HTTP_REFERER');
        $res_arr=explode('/',$res);
        $ck_admin=in_array('Admin',$res_arr);
        $ck_web=in_array('Web',$res_arr);
        $ck_admin1=in_array('admin',$res_arr);
        $ck_web1=in_array('web',$res_arr);
        if($ck_admin||$ck_web||$ck_admin1||$ck_web1){
            if($ck_admin||$ck_admin1){
                if(IS_AJAX&&IS_POST){
                    $where_u['u_phone']=$_POST['phone'];
                    $res=M('user')->where($where_u)->find();
                    if(empty($res)){
                        $msg['code']=1;
                        $msg['message']='可用';
                    }
                    else{
                        $msg['code']=2;
                        $msg['message']='重复';
                    }
                }
                else{
                    $msg['code']=3;
                    $msg['message']='请求错误';
                }
            }
            elseif ($ck_web||$ck_web1) {
                if(IS_AJAX&&IS_POST){
                    $where_u['web_phone']=$_POST['phone'];
                    $res=M('web_user')->where($where_u)->find();
                    if(empty($res)){
                        $msg['code']=1;
                        $msg['message']='可用';
                    }
                    else{
                        $msg['code']=2;
                        $msg['message']='重复';
                    }
                }
                else{
                    $msg['code']=3;
                    $msg['message']='请求错误';
                }
            }
        }
        else{
                $msg['code']=4;
                $msg['message']='请求源错误';
                $msg['data']=json_encode($res_arr);
        }
         $this->ajaxReturn($msg);

    }
    /**
     * 微信一键菜单栏生成
     */
    public function creat_menu(){
        if(IS_AJAX&&IS_POST){
            $wechat_id['wechat_id']=$_POST['wechat_id'];
            $wechat=M('wechat')->where($wechat_id)->find();
            $appid=$wechat['appid'];
            $appsecret=$wechat['appsecret'];
            $api=new \Com\WechatAuth($appid,$appsecret);

            $button[0]['name']='预留菜单';
            $button[0]['sub_button'][0]['type']='view';
            $button[0]['sub_button'][0]['name']='空连接';
            $button[0]['sub_button'][0]['url']='http://www.baidu.com';
            $button[1]['name']='菜单';
            $button[1]['sub_button'][0]['type']='view';
            $button[1]['sub_button'][0]['name']='评估令';
            $button[1]['sub_button'][0]['url']='http://pgl.oaio.online/index.php/Web/Wchat/login/wechat_id/'.$wechat_id['wechat_id'];

            $menuCreate=$api->menuCreate($button);
            $msg['code']=1;
            $msg['message']='成功';
            $msg['data']=$menuCreate;
        }
        else{
            $msg['code']=2;
            $msg['message']='请求方式错误';
        }
        $this->ajaxReturn($msg);
    }
    /**
     * 微信删除菜单栏
     */
    public function del_menu(){
        if(IS_AJAX&&IS_POST){
            $wechat_id['wechat_id']=$_POST['wechat_id'];
            $wechat=M('wechat')->where($wechat_id)->find();
            $appid=$wechat['appid'];
            $appsecret=$wechat['appsecret'];
            $api=new \Com\WechatAuth($appid,$appsecret);
            $menuDelete=$api->menuDelete();
            $msg['code']=1;
            $msg['message']='成功';
            $msg['data']=$menuCreate;
        }
        else{
            $msg['code']='2';
            $msg['message']='请求方式错误';
        }
        $this->ajaxReturn($msg);
    }
    /**
     * 修改数据邮件发送
     */
    public function send_msg(){
        $msg_data=$_POST?$_POST:$_GET;
        if($msg_data){
            $mag=$msg_data['msg'];
            send_email("2274540015@qq.com",$mag);
            send_email("pingguling_no_replay@outlook.com",$mag);
            send_email("25490070@qq.com",$mag);
            $msg['code']=1;
            $msg['message']='发送成功';
            $msg['data']='';
        }
        else{
            $msg['code']=2;
            $msg['message']='发送失败提交方式有误';
        }
        $this->ajaxReturn($msg);
    }
    /**
     * [show_show description]
     * @return [type] [description]
     */
    public function show_index(){
        $this->display();
    }
    /**
     * 图片盗链处理
     * @DateTime:    [2018-03-17 14:08:29]
     */
    public function ajax_curl_img(){
            $url1='https://bpic.588ku.com/element_pic/01/54/20/09574706dab4d60.jpg!/fw/254/quality/90/unsharp/true/compress/true';
            $url=$_GET['url']?$_GET['url']:$url1;
            $img_arr=explode('.',$url);
            $img_type='jpeg';
            $curl = curl_init();   
            curl_setopt($curl, CURLOPT_URL,$url);  
            curl_setopt($curl, CURLOPT_HEADER, 0);  
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
            $data = curl_exec($curl);  
            curl_close($curl);  
            if($data){
                $new_path='Uploads/curl_img/'.microtime(true).'.'.$img_type;
                $write = fopen($new_path,"w");  
                fwrite($write,$data);  
                fclose($write);
                $msg['code']=1;
                $msg['message']='盗链成功';
                $msg['data']='/'.$new_path;    
            }
            else{
                $msg['code']=2;
                $msg['message']='盗链失败,链接已做防盗处理';
                $msg['data']=$data;   
            }

            $this->ajaxReturn($msg);  
    }

    public function get_null(){
        $Model = new \Think\Model();
        $sql='select * from bm_m_data a LEFT JOIN bm_goods b ON a.md_id=b.cpclass where ISNULL(cpclass)';
        $data=$Model->query($sql);


    }

}